package com.ruoyi.project.system.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.SmsResponse;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISendPhoneCodeService;
import com.ruoyi.project.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Classname SmsSenderController
 * @Description 短信发送中心
 * @Author h21448-hxtq
 *
 * @Date 2020-04-09 15 : 33
 * @Version 1.0
 */
@RequestMapping("/auth")
@RestController
@Api(tags = "验证码")
public class SmsSenderController {
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Autowired
    private ISysUserService userService;
    
    @Autowired
    private ISendPhoneCodeService sendCodeService;

    @PostMapping(value = "/sendCode/{phone}")
    @ApiOperation("发送短信验证码")
    public AjaxResult sendSmsCode(@PathVariable(value = "phone") String phone) {
        if (StrUtil.isEmpty(phone)) {
            throw new CustomException("手机号码不能为空");
        }
        List<SysUser> list = userService.list(new LambdaQueryWrapper<SysUser>().eq(SysUser::getPhonenumber, phone.trim()));

        if (CollUtil.isEmpty(list)) {
            throw new CustomException("该手机号码的账户不存在");
        }
        SmsResponse smsResponse = sendCodeService.sendPhoneCode(phone);
        if (ObjectUtil.isNull(smsResponse)) {
            return AjaxResult.success("验证码短信发送失败");
        }
        redisTemplate.opsForValue().set(phone,smsResponse.getSmsCode(),2, TimeUnit.MINUTES);
        return AjaxResult.success();
    }

    @PostMapping(value = "/mailCode/{mailAdress}")
    @ApiOperation("发送邮箱验证码")
    public AjaxResult sendMailCode(@PathVariable(value = "mailAdress") String mailAdress) {
        if (StrUtil.isEmpty(mailAdress)) {
            throw new CustomException("邮箱账号不能为空");
        }
        List<SysUser> list = userService.list(new LambdaQueryWrapper<SysUser>().eq(SysUser::getEmail, mailAdress.trim()));

        if (CollUtil.isEmpty(list)) {
            throw new CustomException("该邮箱还没有进行注册");
        }
        SmsResponse smsResponse = sendCodeService.sendMailCode(mailAdress);
        if (StrUtil.isEmpty(smsResponse.getSmsCode())) {
            return AjaxResult.success("邮箱验证码发送失败");
        }
        redisTemplate.opsForValue().set(mailAdress,smsResponse.getSmsCode(),15, TimeUnit.MINUTES);
        return AjaxResult.success();
    }


}
